﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using GrandMan.Models;
using GrandMan.GMDataSetTableAdapters;

namespace GrandMan.Controllers
{
    public class HomeController : Controller
    {
        //
        // GET: /Home/

        public ActionResult Index()
        {
            HomeModel model = new HomeModel();
            var context = ContextMgt.GetInstance();
            new BaseDataTableAdapter().Fill(context.BaseData);
            new ArticleTableAdapter().Fill(context.Article);
            new ArticleTypeTableAdapter().Fill(context.ArticleType);
            new TeamTableAdapter().Fill(context.Team);
            new PartnerTableAdapter().Fill(context.Partner);

            var companyShort = (from p in context.BaseData
                                where p.Name == "CompanyShort"
                           select p).SingleOrDefault();
            if (companyShort == null || companyShort.IsValueNull())
                model.CompanyShort = string.Empty;
            else
                model.CompanyShort = companyShort.Value;

            var typeList1 = (from a in context.ArticleType
                             where a.ParentID == 4
                             select a.ID);
            var list1 = (from a in context.Article
                         where typeList1.Contains(a.TypeID) &&
                               a.IsHome
                         orderby a.HomeSeq
                         select a).Take(6);
            model.NewsList = new List<HomeArticle>();
            foreach (var item in list1)
                model.NewsList.Add(new HomeArticle() { Title = item.Name, Path = string.Format(@"\Home\Article?id={0}", item.ID) });

            var typeList2 = (from a in context.ArticleType
                             where a.ParentID == 5
                             select a.ID);
            var list2 = (from a in context.Article
                         where typeList2.Contains(a.TypeID) &&
                               a.IsHome
                         orderby a.HomeSeq
                         select a).Take(13);
            model.InfoList = new List<HomeArticle>();
            foreach (var item in list2)
                model.InfoList.Add(new HomeArticle() { Title = item.Name, Path = string.Format(@"\Home\Article?id={0}", item.ID) });

            var list3 = (from a in context.Article
                         where a.TypeID == 6 &&
                               a.IsHome
                         orderby a.HomeSeq
                         select a).Take(6);
            model.CaseList = new List<HomeArticle>();
            foreach (var item in list3)
                model.CaseList.Add(new HomeArticle() { Title = item.Name, Path = string.Format(@"\Home\Article?id={0}", item.ID) });

            model.TeamList = (from a in context.Team
                              orderby a.Seq
                              select a).Take(4).ToList();

            model.PartnerList = (from a in context.Partner
                                 orderby a.Seq
                                 select a).ToList();

            return View(model);
        }

        public ActionResult EditorTest()
        {
            EditorModel model = new EditorModel();
            return View(model);
        }

        [HttpPost]
        public ActionResult EditorTest(EditorModel model)
        {
            model = new EditorModel();
            return View(model);
        }

        public ActionResult Case()
        {
            Type1Model model = new Type1Model();
            var context = ContextMgt.GetInstance();
            new ArticleTableAdapter().Fill(context.Article);
            model.List = (from a in context.Article
                         where a.TypeID == 6
                         orderby a.Seq
                         select a).ToList();
            return View(model);
        }

        public ActionResult Company()
        {
            var context = ContextMgt.GetInstance();
            new ArticleTableAdapter().Fill(context.Article);
            var article = (from a in context.Article
                           where a.TypeID == 1
                           select a).FirstOrDefault();
            ArticleModel model = new ArticleModel();
            if (article == null)
            {
                model.Title = "广州广民企业管理咨询有限公司";
                model.Content = string.Empty;
            }
            else
            {
                model.Title = string.Format("{0} - {1}", "广州广民企业管理咨询有限公司", article.Name);
                model.Content = article.Content;
            }

            return View(model);
        }

        public ActionResult Contact()
        {
            var context = ContextMgt.GetInstance();
            new ArticleTableAdapter().Fill(context.Article);
            var article = (from a in context.Article
                           where a.TypeID == 3
                           select a).FirstOrDefault();
            ArticleModel model = new ArticleModel();
            if (article == null)
            {
                model.Title = "广州广民企业管理咨询有限公司";
                model.Content = string.Empty;
            }
            else
            {
                model.Title = string.Format("{0} - {1}", "广州广民企业管理咨询有限公司", article.Name);
                model.Content = article.Content;
            }

            return View(model);
        }

        public ActionResult Team()
        {
            Type1Model model = new Type1Model();
            var context = ContextMgt.GetInstance();
            new ArticleTableAdapter().Fill(context.Article);
            model.List = (from a in context.Article
                          where a.TypeID == 2
                          orderby a.Seq
                          select a).ToList();
            return View(model);
        }

        public ActionResult News(int? id)
        {
            Type2Model model = new Type2Model();
            var context = ContextMgt.GetInstance();
            new ArticleTypeTableAdapter().Fill(context.ArticleType);
            new ArticleTableAdapter().Fill(context.Article);
            model.ColumnList = (from a in context.ArticleType
                                where a.ParentID == 4
                                orderby a.Seq
                                select a).ToList();
            if (id == null)
            {
                if (model.ColumnList.Count > 0)
                {
                    model.ArticleList = (from a in context.Article
                                         where a.TypeID == model.ColumnList[0].ID
                                         orderby a.Seq
                                         select a).ToList();
                }
                else
                    model.ArticleList = new List<GMDataSet.ArticleRow>();
            }
            else
                model.ArticleList = (from a in context.Article
                                     where a.TypeID == id
                                     orderby a.Seq
                                     select a).ToList();
            return View(model);
        }

        public ActionResult Info(int? id)
        {
            Type2Model model = new Type2Model();
            var context = ContextMgt.GetInstance();
            new ArticleTypeTableAdapter().Fill(context.ArticleType);
            new ArticleTableAdapter().Fill(context.Article);
            model.ColumnList = (from a in context.ArticleType
                                where a.ParentID == 5
                                orderby a.Seq
                                select a).ToList();
            if (id == null)
            {
                if (model.ColumnList.Count > 0)
                {
                    model.ArticleList = (from a in context.Article
                                         where a.TypeID == model.ColumnList[0].ID
                                         orderby a.Seq
                                         select a).ToList();
                }
                else
                    model.ArticleList = new List<GMDataSet.ArticleRow>();
            }
            else
                model.ArticleList = (from a in context.Article
                                     where a.TypeID == id
                                     orderby a.Seq
                                     select a).ToList();
            return View(model);
        }

        public ActionResult Article(int id)
        {
            var context = ContextMgt.GetInstance();
            new ArticleTableAdapter().Fill(context.Article);
            var article = (from a in context.Article
                           where a.ID == id
                           select a).SingleOrDefault();
            ArticleModel model = new ArticleModel();
            model.Title = string.Format("{0} - {1}","广州广民企业管理咨询有限公司",article.Name);
            model.Content = article.Content;

            return View(model);
        }
    }
}
